import service from "@/utils/request";
import { ElMessage } from "element-plus";

import type { Blog, BlogsResponse } from "@/types/blog";
import { defaultBlog } from "@/types/blog";

function getBlogs(id: number, count: number) {
  return service({
    url: `/blogs?id=${id}&count=${count}`,
    method: "get",
  });
}

export async function apiGetBlogs(
  id: number,
  count: number
): Promise<BlogsResponse> {
  const rsp: any = await getBlogs(id, count);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data as BlogsResponse;
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Get blogs failed",
      type: "error",
      center: true,
    });

    return {
      total_count: 0,
      blog_count: 0,
      blogs: [],
    };
  }
}

function getBlog(blogId: number){
  return service({
    url: `/blog?id=${blogId}`,
    method: "get",
  });
}

export async function apiGetBlog(blogId: number): Promise<Blog> {
  const rsp: any = await getBlog(blogId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data as Blog;
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "获取博客失败",
      type: "error",
      center: true,
    });

    return defaultBlog();
  }
}

function addBlog(data: Blog) {
  return service({
    url: "/blog",
    method: "post",
    data: data,
  });
}

export async function apiAddBlog(newBlog: Blog): Promise<number> {
  const rsp: any = await addBlog(newBlog);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data as number;
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "新增博客失败",
      type: "error",
      center: true,
    });

    return 0;
  }
}

function viewInc(blogId: number){
  return service({
    url: `/blog?id=${blogId}/star-inc`,
    method: "get",
  });
}

export async function apiViewInc(blogId: number): Promise<number> {
  const rsp: any = await viewInc(blogId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["views"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Blog view inc failed",
      type: "error",
      center: true,
    });

    return -1;
  }
}

function starInc(blogId: number){
  return service({
    url: `/blog?id=${blogId}/star-inc`,
    method: "get",
  });
}

export async function apiStarInc(blogId: number): Promise<number> {
  const rsp: any = await starInc(blogId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["stars"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Blog star inc failed",
      type: "error",
      center: true,
    });

    return -1;
  }
}

function starDec(blogId: number){
  return service({
    url: `/blog?id=${blogId}/star-dec`,
    method: "get",
  });
}

export async function apiStarDec(blogId: number): Promise<number> {
  const rsp: any = await starDec(blogId);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message)
    }

    return rsp.data["stars"];
  } else {
    ElMessage({
      message: rsp.message != "" ? rsp.message : "Blog star dec failed",
      type: "error",
      center: true,
    });

    return -1;
  }
}
